﻿using Developer.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Developer.Service
{
    public class BookService : BaseService<Books>
    {
        public static void Remove(int id)
        {
            var book = SysInfo.DB.Books.Find(id);
            book.IsAbort = false;
            SysInfo.DB.SaveChanges();
        }
    }

   
    public class OrderService : BaseService<Orders>
    {
        /// <summary>
        /// 改变订单状态
        /// </summary>
        /// <param name="id">OrderId</param>
        /// <param name="state">状态：  0 => 未完成； 1 => 已完成</param>
        public static void ChangeState(int id,int state)
        {
            var order = SysInfo.DB.Orders.Find(id);
            order.State = state;
            SysInfo.DB.SaveChanges();
        }
    }
    public class BorrowListService : BaseService<Borrowings>
    {
        public static void ReturnBook(int id)
        {
            var book = SysInfo.DB.Borrowings.Find(id);
            book.ReturnTime = DateTime.Now;
            SysInfo.DB.SaveChanges();
        }
    }

    public class OrderTempService : BaseService<OrderTemps>
    {
        public static void EndOrderTemp(int id)
        {
            var book = SysInfo.DB.OrderTemps.Find(id);
            book.IsAbort = true;
            SysInfo.DB.SaveChanges();
        }
    }

    public class MessageService : BaseService<Messages>
    {
        public static void SetReaded(int id)
        {
            var message = SysInfo.DB.Messages.Find(id);
            message.IsRead = true;
            SysInfo.DB.SaveChanges();
        }
        public static void BatSend(string [] ids,Messages message)
        {
            foreach(var item in ids)
            {
                message.UserId = int.Parse(item);
                SysInfo.DB.Messages.Add(message);
            }
            SysInfo.DB.SaveChanges();
        }
    }

    public class FavoriteService : BaseService<Favorites>
    {
        public static void Remove(int id)
        {
            var fa = SysInfo.DB.Favorites.Find(id);
            SysInfo.DB.Favorites.Remove(fa);
            SysInfo.DB.SaveChanges();
        }
    }

    public class FeedBackService : BaseService<FeedBacks>
    {
        /// <summary>
        /// 管理员回复
        /// </summary>
        /// <param name="content">回复内容</param>
        /// <param name="id">Feedback Id</param>
        public static void ReBack(string content, int id)
        {
            var feedBack = SysInfo.DB.FeedBacks.Find(id);
            feedBack.ReContent = content;
            SysInfo.DB.SaveChanges();
        }
        /// <summary>
        /// 用户添加反馈
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="uid">用户id</param>
        public static void AddFeedBack(string content, int uid)
        {
            FeedBacks f = new FeedBacks
            {
                Content = content,
                UserId = uid,
                CreateTime = DateTime.Now,
                State = 0
            };
            SysInfo.DB.FeedBacks.Add(f);
            SysInfo.DB.SaveChanges();
        }
        /// <summary>
        /// 改变反馈状态
        /// </summary>
        /// <param name="state">状态 0: 未读    1:已读</param>
        /// <param name="fid"></param>
        public static void ChangeState(int state, int fid)
        {
            var feed = SysInfo.DB.FeedBacks.Find(fid);
            feed.State = state;
            SysInfo.DB.SaveChanges();
        }
    }

    public class BookTypeService : BaseService<BookTypes>
    {

    }

    public class ReviewService : BaseService<Reviews>
    {
        public static void Remove(int id)
        {
            var review = SysInfo.DB.Reviews.Find(id);
            SysInfo.DB.Reviews.Remove(review);
            SysInfo.DB.SaveChanges();
        }

       
    }

    public class CommentService : BaseService<Comments>
    {
        public static void Remove(int id)
        {
            var comm = SysInfo.DB.Comments.Find(id);
            SysInfo.DB.Comments.Remove(comm);
            SysInfo.DB.SaveChanges();
        }
    }

    public class SystemService : BaseService<SystemInfoes>
    {
        public static void login(string IP,DateTime time)
        {
            SysInfo.DB.SystemInfoes.Add(new SystemInfoes
            {
                IPAddress = IP,
                LoginTime = time
            });
            SysInfo.DB.SaveChanges();
        }
    }
}

